package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.musicroom.MusicRoomFurnitureInfo;
import com.yanqu.road.entity.musicroom.UserMusicRoomBuildingData;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserMusicRoomBuildingDaoImpl extends TempDataObjectDao<UserMusicRoomBuildingData> {
    public static String getUserMusicRoomBuildingDataSql = "select * from `t_u_music_room_building` where `userId`=?";

    public Map<Integer, UserMusicRoomBuildingData> getUserMusicRoomBuildingData(long userId) {
        Map<Integer, UserMusicRoomBuildingData> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserMusicRoomBuildingDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserMusicRoomBuildingData data = parse(rs);
                if (null != data) {
                    dataMap.put(data.getBuildId(), data);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateUserMusicRoomBuildingDataSql = "update `t_u_music_room_building` set amountReward=?,furnitureInfos=? where userId=? and buildId=?";

    @Override
    protected boolean updateData(UserMusicRoomBuildingData userMusicRoomBuildingData) {
        userMusicRoomBuildingData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(userMusicRoomBuildingData.getAmountReward())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userMusicRoomBuildingData.getFurnitureInfos())));

        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBuildingData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomBuildingData.getBuildId()));
        if (getDb().executeNoneQuery(updateUserMusicRoomBuildingDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserMusicRoomBuildingDataSql = "insert into `t_u_music_room_building`(userId,buildId,amountReward,furnitureInfos) values (?,?,?,?)";

    @Override
    protected boolean addData(UserMusicRoomBuildingData userMusicRoomBuildingData) {
        userMusicRoomBuildingData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userMusicRoomBuildingData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userMusicRoomBuildingData.getBuildId()));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(userMusicRoomBuildingData.getAmountReward())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userMusicRoomBuildingData.getFurnitureInfos())));
        if (getDb().executeNoneQuery(insertUserMusicRoomBuildingDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserMusicRoomBuildingData parseData(ResultSet rs) throws SQLException {
        UserMusicRoomBuildingData userMusicRoomBuildingData = new UserMusicRoomBuildingData();
        userMusicRoomBuildingData.setUserId(rs.getLong("userId"));
        userMusicRoomBuildingData.setBuildId(rs.getInt("buildId"));
        String amountReward = rs.getString("amountReward");
        if (!StringUtils.isNullOrEmpty(amountReward)) {
            userMusicRoomBuildingData.setAmountReward(PropertyHelper.parseStringToProperty(amountReward));
        }
        userMusicRoomBuildingData.setFurnitureInfos(JSON.parseObject(rs.getString("furnitureInfos"), new TypeReference<List<MusicRoomFurnitureInfo>>() {
        }));

        return userMusicRoomBuildingData;
    }
}
